<!-- 节目管理 -->
<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item :label="$t('equipment.name')" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          :placeholder="$t('equipment.nameInput')"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('equipment.programName')" prop="programName">
        <el-input
          v-model="queryParams.programName"
          :placeholder="$t('equipment.programNameInput')"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('terminal.organization')" prop="deptId">
        <treeselect v-model="queryParams.deptId" :options="deptOptions" :default-expand-level="2" :normalizer="deptNormalizer" style="width: 198px;" placeholder="请选择组织机构" />
      </el-form-item>
      <el-form-item :label="$t('elButton.establishTime')">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          :start-placeholder="$t('elButton.startTime')"
          :end-placeholder="$t('elButton.endTime')"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t('elButton.search') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('elButton.reset') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPerm="['playRecord_del']"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDel"
        >{{ $t('elButton.delete') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPerm="['playRecord_export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" :content="$t('elButton.refresh')" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery" />
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? $t('elButton.hideSearch') : $t('elButton.showSearch')" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch" />
        </el-tooltip>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="playRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('equipment.number')" width="80" align="center" prop="id" />
      <el-table-column :label="$t('equipment.identification')" align="center" prop="deviceCode" />
      <el-table-column :label="$t('equipment.name')" align="center" prop="deviceName" />
      <el-table-column :label="$t('program.programNumber')" align="center" prop="programId" />
      <el-table-column :label="$t('equipment.programName')" align="center" prop="programName" />
      <el-table-column :label="$t('equipment.programTimeWiths')" align="center" prop="duration" />
      <el-table-column :label="$t('terminal.playbackTimes')" align="center" prop="total" />
      <el-table-column :label="$t('program.playTimeWiths')" align="center" prop="palyTime" />
      <el-table-column :label="$t('equipment.control')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPerm="['playRecord_view']"
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleDetail(scope.row)"
          >明细
          </el-button>
          <el-button
            v-hasPerm="['playRecord_del']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDel(scope.row)"
          >{{ $t('elButton.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" :fullscreen="true" :modal-append-to-body="true" :append-to-body="true">
      <el-table
        :data="detailList"
        style="width: 100%"
        :border="true"
        :stripe="true"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column :label="$t('equipment.identification')" align="center" prop="deviceCode" />
        <el-table-column :label="$t('equipment.name')" align="center" prop="deviceName" />
        <el-table-column :label="$t('program.programNumber')" align="center" prop="programId" />
        <el-table-column :label="$t('equipment.programName')" align="center" prop="programName" />
        <el-table-column label="节目场景" align="center" prop="scene" />
        <el-table-column :label="$t('equipment.programTimeWiths')" align="center" prop="duration" />
        <el-table-column label="上传时间" align="center" prop="createTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="detailTotal>0"
        :total="detailTotal"
        :page.sync="detailParams.current"
        :limit.sync="detailParams.size"
        @pagination="getDetailList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  delPlayRecord,
  playStatList,
  playDetailList,
  exportPlayRecord
} from '@/api/ips/playRecord'
import { deptTree } from '@/api/system/dept'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'PlayRecord',
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 播放记录表格数据
      playRecordList: [],
      detailTotal: 0,
      detailList: [],
      // 机构名称树选项
      deptOptions: undefined,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        deviceName: undefined,
        programName: undefined,
        createTime: undefined
      },
      detailParams: {
        current: 1,
        size: 10,
        deviceCode: undefined,
        programId: undefined
      },
      // 显示搜索条件
      showSearch: true,
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
    this.getDeptTree()
  },
  methods: {
    /** 查询播放记录列表 */
    getList() {
      this.loading = true
      playStatList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.playRecordList = response.data
        this.total = response.total
        this.loading = false
      })
    },
    getDetailList() {
      this.loading = true
      playDetailList(this.addDateRange(this.detailParams, this.dateRange)).then(response => {
        this.detailList = response.data
        this.detailTotal = response.total
        this.loading = false
      })
    },
    /** 转换机构数据结构 */
    deptNormalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.deptId,
        label: node.name,
        children: node.children
      }
    },
    /** 查询机构名称下拉树结构 */
    getDeptTree() {
      deptTree().then(response => {
        if (response.data && response.data.length > 0) {
          this.deptOptions = this.handleTree(response.data, 'deptId', 'parentId', 'children', response.data[0].parentId)
        } else {
          this.deptOption = []
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        deviceCode: undefined,
        programId: undefined,
        createTime: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleDetail(row) {
      this.open = true
      this.title = '设备：' + row.deviceName + ' 播放明细'
      this.detailParams.deviceCode = row.deviceCode
      this.detailParams.programId = row.programId
      this.getDetailList()
    },
    /** 删除按钮操作 */
    handleDel(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除播放记录编号为"' + ids + this.$t('equipment.deleteEquipment2'), this.$t('warning'), {
        confirmButtonText: this.$t('submitText'),
        cancelButtonText: this.$t('cancelText'),
        type: 'warning'
      }).then(function() {
        return delPlayRecord(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('deleteSuccess'))
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.addDateRange(this.queryParams, this.dateRange)
      this.$confirm('是否确认导出所有播放记录数据项?', this.$t('warning'), {
        confirmButtonText: this.$t('submitText'),
        cancelButtonText: this.$t('cancelText'),
        type: 'warning'
      }).then(function() {
        return exportPlayRecord(queryParams)
      }).then(response => {
        this.download(response.data)
      }).catch(function() {
      })
    }
  }
}
</script>
